<?php

class Category extends CI_Controller {

	public function __construct() {

		parent::__construct();
		$this->output->enable_profiler(PROFILER);
		is_logged_in();
		$this->load->model("categorymodel");
	}

	public function index() {
		$this -> load -> library('table');

		if ($this -> input -> post('search')) {

			if (array_key_exists('search', $_POST)) {

				$term = $this -> input -> post('search');
				//disable pagination
				$data['pagination_enabled'] = false;

			}

			if ($query = $this -> categorymodel -> search($term)) {
				$data['categories'] = $query;
			}

		} else {

			
			$this -> load -> library('pagination');

			//enable pagination
			$data['pagination_enabled'] = true;

			$config['base_url'] = site_url() . '/category/index';
			$config['total_rows'] = $this -> db -> get('blog_category') -> num_rows();
			$config['per_page'] = 10;
			$config['num_links'] = 20;
			$this -> pagination -> initialize($config);
			if ($this -> uri -> segment(3)) {
				$offset = $this -> uri -> segment(3);
			}
			else{
				$offset=0;
			}

			if ($query = $this -> categorymodel -> getAll($config['per_page'], $offset)) {
				$data['categories'] = $query;
			}

		}

		$data['main_content'] = "admin/category/index";
		$this -> load -> view("includes/dashboard/template", $data);

	}

	public function create() {

		if ($this -> form_validation -> run('category/create') == TRUE) {

			$id = $this -> categorymodel -> add($_POST);

			if ($id) {

				$this -> session -> set_flashdata('message', array('title' => 'Category Created', 'content' => 'The category has been sucessfully created', 'type' => 'alert_success'));
				redirect('category/index');

			} else {

				$this -> session -> set_flashdata('message', array('title' => 'Category Not Created', 'content' => 'A database Error has occur, please contact your administrator', 'type' => 'alert_error'));
				redirect('category/index');
			}

		} else {
			$data['main_content'] = "admin/category/form";
			$this -> load -> view("includes/dashboard/template", $data);

		}
	}

	public function edit($id) {

		$id = (int)$id;
		$data['id'] = $id;
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {

			if ($this -> form_validation -> run("category/edit") == TRUE) {

				if ($this -> categorymodel -> updaterecord($_POST, $id)) {

					$this -> session -> set_flashdata('message', array('title' => 'Category Updated', 'content' => 'The category has been sucessfully updated', 'type' => 'alert_success'));
					redirect('category/index');

				} else {

					$this -> session -> set_flashdata('message', array('title' => 'Category Not Updated', 'content' => 'A database Error has occur, please contact your administrator', 'type' => 'alert_error'));
					redirect('category/index');
				}

			} else {

				$data['category'] = array();
				$data['main_content'] = "admin/category/form";
				$this -> load -> view("includes/dashboard/template", $data);
			}

		} else {


			$data['category'] = $this -> categorymodel -> getById($id);
			if ($data['category']) {
				$data['main_content'] = "admin/category/form";
				$this -> load -> view("includes/dashboard/template", $data);
			} else {
				$this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve category registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => 'alert_warning'));
				redirect('category/index');
			}
		}
	}

	public function delete($id) {
		$this -> categorymodel -> delete($id);
		$this -> session -> set_flashdata('message', array('title' => 'Category deleted', 'content' => 'The category has been sucessfully deleted', 'type' => 'alert_success'));
		redirect('category/index');
	}

}
?>